package com.bdqn.zhao.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.zhao.pojo.IpPark;
import com.bdqn.zhao.pojo.SysDept;
import com.bdqn.zhao.pojo.SysRole;
import com.bdqn.zhao.pojo.SysUser;
import com.bdqn.zhao.service.SysUserService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author zhangsan
 * @since 2023-04-24
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    public SysUserService sysUserService;

    /**
     * 用户全查操作
     * @return
     */
    @GetMapping("/user")
    public List seleceAll() {
        return sysUserService.selectAllJ();
    }

   /**
     * 根据id详细查询
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    public SysUser selectbyid(@PathVariable int id, HttpSession session){
        SysUser selectbyid = sysUserService.selectbyidJ(id);
        session.setAttribute("",selectbyid);
        return selectbyid;

    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @GetMapping("/user/del/{id}")
    public  int delectSysuer(@PathVariable int id){
        return sysUserService.delectSysuer(id);
    }

    /**
     * 根据用户名修改密码
     * @param sysUser
     * @return
     */
    @PostMapping("/user/update/pwd")
    public int update(SysUser sysUser){
        return sysUserService.updatePwd(sysUser);
    }

    @PostMapping("/user/add")
    public int sysuerAdd(SysUser sysUser){
        return sysUserService.sysuserAdd(sysUser);

    }
    @GetMapping("/SysDeotlist")
    public Map<String,Object> stringObjectMap(){
        List<SysDept> department = sysUserService.department();
        Map<String,Object> map=new HashMap<>();
        map.put("Deptlist",department);
        return map;
    }
    @GetMapping("/IpParklist")
    public Map<String,Object> stringObjectMap1(){
        List<IpPark> department = sysUserService.departmentyq();
        Map<String,Object> map=new HashMap<>();
        map.put("IpParklist",department);
        return map;
    }
    @GetMapping("/SysRolelist")
    public Map<String,Object> stringObjectMap2(){
        List<SysRole> department = sysUserService.departmentjs();
        Map<String,Object> map=new HashMap<>();
        map.put("IpParklist",department);
        return map;
    }

    /**
     * 返回新增界面
     * @return
     */
    @GetMapping("user/newadd")
    public String newadd(){
        return "";
    }

    //用户设置  修改
    @PostMapping("/yhUpdate")
    @ResponseBody
    public String yhxg(String login_name,String user_name, String mobile,String gender,String post,String email){
        SysUser sysUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq("login_name",login_name));


        if (sysUser!=null){
            sysUser.setUserName(user_name);
            sysUser.setMobile(mobile);
            sysUser.setGender(gender);
            sysUser.setPost(post);
            sysUser.setEmail(email);
            if (sysUserService.updateById(sysUser)){
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
        }else {
            System.out.println("用户名错误");
        }
        return "";
    }

    //修改密码
    @PostMapping("/updatePassword")
    @ResponseBody
    public String xgmm(Integer id, String password, String xpassword, HttpSession session){
        String a= (String) session.getAttribute("password");
//        if (a==password){
//            SysUser sysUser = sysUserService.getOne()
//        }
        SysUser sysUser = new SysUser();
        sysUser.setUserId(id);
        sysUser.setPassword(xpassword);
        if (sysUserService.updateById(sysUser)){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
        return "";
    }

}

